package com.imooc.threadexample;

/**
 * Created by yijy on 2017/11/17.
 */
public class EnergyThread implements Runnable {

    private EnergySystem energySystem;

    private int fromBox;//目标能量盒子下标

    private double MaxEnergy;//每次转移最大能量

    private static final int Delay = 10;

    public EnergyThread(EnergySystem energySystem,int from,double initEnergy){
        this.energySystem = energySystem;
        this.fromBox = from;
        this.MaxEnergy = initEnergy;
    }


    @Override
    public void run() {
            while (true){
                double amount = MaxEnergy *Math.random();
                int to = (int) (energySystem.getEnergyLength()*Math.random());
                energySystem.transferEnergy(fromBox,to,amount);
                try {
                    Thread.sleep((int)(Delay*Math.random()));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

    }
}
